package com.acc.controller;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.acc.entity.EasyUIDataGridJSON;
import com.acc.entity.Standby;
import com.acc.service.UserStandbyService;
import com.acc.service.impl.UserStandbyServiceImpl;
import com.alibaba.fastjson.JSON;


/**
 * 
 * @author 夏艳
 *
 */
public class UserStandbyController extends InnerDispacherController {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void pageDataGrid(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Integer page = Integer.valueOf(request.getParameter("page"));
		Integer rows = Integer.valueOf(request.getParameter("rows"));
		
		System.out.println("page:" + page + ";rows:" + rows);
		
		int start = 0;
		
		String departure=request.getParameter("startLocation");
		String destination=request.getParameter("endLocation");
		String data_str = request.getParameter("startTime");
		System.out.println("data_str:" + data_str);
		UserStandbyService userService=new UserStandbyServiceImpl();
		List<Standby> data=new ArrayList<Standby>();
		if(null==departure&&null==destination&&null==data_str) {
			 data= userService.showAlltickets();
		}
		
		else if(null!=departure&&null!=destination&&null!=data_str) {
			SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
			
		    java.util.Date start_time=null;
			try {
				
				start_time = dateFormat.parse(data_str);
				
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//通过new Date(long time)构造方法吧 一个长整型的时间戳转为java.sql.Date类型
			java.sql.Date starttime= new java.sql.Date(start_time.getTime());
			
			data=userService.queryTickets(departure, destination, starttime);
		}
		
		List<Standby> tickets = new ArrayList<>();
		if (page == 1) {
			start = 0;
		}
		else {
			start =(page-1)*rows;
		}
		if(page*rows<data.size()) {
			for (int i = start ; i < start + rows; i++) {
				tickets.add(data.get(i));
			}
		}
		else {
			for (int i = start ; i < data.size(); i++) {
				tickets.add(data.get(i));
			}
		}
			
		
		
		EasyUIDataGridJSON responseObject = new EasyUIDataGridJSON();
		responseObject.setTotal(data.size());
		responseObject.setRows(tickets);
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/json");
		response.getWriter().append(JSON.toJSONString(responseObject));
		System.out.println(JSON.toJSONString(responseObject));
	}
	
	
	

}




